package com.easysoft.framework.product.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.product.mapper.SysProductCategoryMapperExtd;
import com.easysoft.framework.product.model.SysProductCategory;
import com.easysoft.framework.product.model.SysProductCategoryQuery;
import com.easysoft.framework.product.model.SysProductCategoryView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("service.SysProductCategoryService")
public class SysProductCategoryService {
    @Autowired
    @Qualifier("mapper.SysProductCategoryMapperExtd")
    private SysProductCategoryMapperExtd sysProductCategoryMapperExtd;

    @DataSource(DataSource.SLAVE)
    public List<SysProductCategoryView> selectSysProductCategoryByPage(Page page, SysProductCategoryQuery query) {
        int maxRow = sysProductCategoryMapperExtd.selectCountAllProductCategory(query);
        if (maxRow == 0){
            return new ArrayList<SysProductCategoryView>();
        }
        page.setMaxRow(maxRow);
        return sysProductCategoryMapperExtd.selectSysProductCategoryByPage(page,query);
    }

    @DataSource(DataSource.MASTER)
    public int addSysProductCategory(SysProductCategory sysProductCategory) {
        sysProductCategory.setId(UUIDUtils.getUUID());
        sysProductCategory.setCreateTime(new Date());
        return sysProductCategoryMapperExtd.insertSelective(sysProductCategory);
    }

    @DataSource(DataSource.SLAVE)
    public SysProductCategory selectSysProductCategoryById(String id) {
        return sysProductCategoryMapperExtd.selectByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int updateSysProductCategory(SysProductCategory sysProductCategory){
        return sysProductCategoryMapperExtd.updateByPrimaryKeySelective(sysProductCategory);
    }

    @DataSource(DataSource.MASTER)
    public int deleteSysProductCategory(String id) {

        return sysProductCategoryMapperExtd.deleteByPrimaryKey(id);
    }

    @DataSource(DataSource.SLAVE)
    public List<SysProductCategoryView> selectSysProductCategory(boolean onlyRoot) {
        return sysProductCategoryMapperExtd.selectSysProductCategory(onlyRoot);
    }
}
